package main;

import java.util.Random;

import game.Map;
import game.Tank;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

public class PlayingCanvas extends GameCanvas implements Runnable {

	public static int SCREEN_WIDTH;
	public static int SCREEN_HEIGHT;

	boolean isPlaying = true;
	MainMidlet midlet;
	LayerManager layerManager;
	int viewportX;
	int viewportY;

	boolean pressed;
	int pressX;
	int pressY;
	Tank tank;
	Map map;

	protected PlayingCanvas(MainMidlet midlet) {
		super(true);
		setFullScreenMode(true);
		this.midlet = midlet;

		SCREEN_WIDTH = getWidth();
		SCREEN_HEIGHT = getHeight();

		viewportX = 0;
		viewportY = 0;
		pressed = false;

		layerManager = new LayerManager();

		// int[][] matrix = {
		// { 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		// { 1, 1, 2, 1, 1, 1, 1, 1, 6 },
		// { 1, 1, 1, 3, 1, 1, 1, 1, 1 },
		// { 1, 1, 1, 1, 1, 4, 1, 5, 1 },
		// { 1, 1, 1, 1, 1, 4, 1, 5, 1 },
		// { 1, 1, 1, 1, 1, 4, 1, 5, 1 },
		// { 1, 1, 1, 1, 1, 4, 1, 5, 1 },
		// { 1, 1, 1, 1, 1, 4, 1, 5, 1 },
		// { 1, 1, 1, 1, 1, 1, 1, 1, 1 } };

		int rows = 100;
		int cols = 100;
		int matrix[][] = new int[rows][cols];
		Random random = new Random();
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				matrix[i][j] = random.nextInt(6) + 1;
			}
		}

		map = new Map(matrix);
		map.addToLayer(layerManager);

		tank = new Tank();
		tank.init(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);

		lastPosX = tank.getX();
		lastPosY = tank.getY();

		tank.addToLayer(layerManager);
	}

	/*
	 * public void run() { Graphics g = getGraphics(); while(isPlaying){
	 * 
	 * update(); display(g); try { Thread.sleep(50); } catch
	 * (InterruptedException e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); } } }
	 */
	public void run() {
		Graphics g = getGraphics();
		long startTime, endTime, delay;
		while (isPlaying) {
			startTime = System.currentTimeMillis();
			update();
			display(g);
			endTime = System.currentTimeMillis();
			// repaint();
			try {
				delay = 50 - (endTime - startTime);
				if (delay > 0) {
					Thread.sleep(delay);
				}
			} catch (InterruptedException ex) {
			}
		}
	}

	public void start() {
		isPlaying = true;
		Thread t = new Thread(this);
		t.start();
	}

	public void stop() {
		isPlaying = false;
	}

	int lastPosX, lastPosY;

	public void update() {
		if (pressed) {
			tank.run(pressX - viewportX - tank.getX(), pressY - viewportY
					- tank.getY());
		}

		tank.update();
		
		if(tank.getX() <= 20 || tank.getX() - viewportX >= map.getWidth() - 20
				|| tank.getY() <= 20 || tank.getY() - viewportY >= map.getHeight() - 20){
			tank.revertVelocity(0.5);
		}

		updateCamera();
	}

	private void updateCamera() {
		if (tank.getX() - viewportX >= SCREEN_WIDTH / 2
				&& tank.getX() - viewportX <= map.getWidth() - SCREEN_WIDTH / 2)
			viewportX += lastPosX - tank.getX();

		if (tank.getY() - viewportY >= SCREEN_HEIGHT / 2
				&& tank.getY() - viewportY <= map.getHeight() - SCREEN_HEIGHT
						/ 2)
			viewportY += lastPosY - tank.getY();

		if (viewportX > 0)
			viewportX = 0;
		if (viewportY > 0)
			viewportY = 0;

		lastPosX = tank.getX();
		lastPosY = tank.getY();
	}

	public void display(Graphics g) {

		layerManager.paint(g, viewportX, viewportY);

		flushGraphics();
	}

	protected void pointerPressed(int x, int y) {
		pressed = true;
		pressX = x;
		pressY = y;
	}

	protected void pointerReleased(int x, int y) {
		// midlet.exit();
		pressed = false;
	}

	protected void pointerDragged(int x, int y) {
		pressX = x;
		pressY = y;
	}
}
